package com.kc.saf.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kc.saf.domain.SafetyAlertType;
import com.kc.saf.service.SafetyAlertTypeService;
import com.kc.saf.vo.SafetyAlertTypeVo;
import com.kc.sys.common.DataGridView;
import com.kc.sys.common.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 老杜
 * @since 2019-11-06
 */
@RestController
@RequestMapping("/safetyAlertType")
public class SafetyAlertTypeController {

    @Autowired
    private SafetyAlertTypeService safetyAlertTypeService;

    /**
     * 查询
     */
    @RequestMapping("loadAllSafetyAlertType")
    public DataGridView loadAllSafetyAlertType(SafetyAlertTypeVo safetyAlertTypeVo){
        //获取用户session
        //User user = (User)WebUtils.getSession().getAttribute("user");
        IPage<SafetyAlertType> page = new Page<>(safetyAlertTypeVo.getPage(),safetyAlertTypeVo.getLimit());
        QueryWrapper<SafetyAlertType> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(safetyAlertTypeVo.getTypename()),"typename",safetyAlertTypeVo.getTypename());
        safetyAlertTypeService.page(page,queryWrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }
    //添加公告
    @RequestMapping("addSafetyAlertType")
    public R addSafetyAlertType(SafetyAlertTypeVo safetyAlertTypeVo){
        try {
            safetyAlertTypeVo.setCreatetime(new Date());
            safetyAlertTypeService.save(safetyAlertTypeVo);
            return R.ok().message("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("添加失败");
        }
    }
    //更新公告
    @RequestMapping("updateSafetyAlertType")
    public R updateSafetyAlertType(SafetyAlertTypeVo safetyAlertTypeVo){
        try {
            safetyAlertTypeService.updateById(safetyAlertTypeVo);
            return R.ok().message("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("更新失败");
        }
    }
    /**
     * 根据Id 删除
     * */
    @RequestMapping("deleteSafetyAlertType")
    public R deleteById(Integer id){
        try {
            safetyAlertTypeService.removeById(id);
            return R.ok().message("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("删除失败");
        }
    }
    /**
     * 查询条件
     * */
    @RequestMapping("loadAllsafetyAlertTypeForSelect")
    public DataGridView loadAllsafetyAlertTypeForSelect(){
        List<SafetyAlertType> list = safetyAlertTypeService.list(null);
        return new DataGridView(list);
    }
}

